/**
 * 	 Copyright (c) 2008, 2009 Lukas Zaruba
 * 
 *   This file is part of UniAnalyzer (Universal Analyzer).
 *
 *   UniAnalyzer is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   UniAnalyzer is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with UniAnalyzer. If not, see <http://www.gnu.org/licenses/>.
 */

package net.zarubsys.unianalyzer.dataminers.lpt.internal.win32;

import net.zarubsys.unianalyzer.dataminers.lpt.internal.exceptions.UnianalyzerPortException;
import net.zarubsys.unianalyzer.dataminers.lpt.internal.provider.ILptProvider;

import org.apache.log4j.Logger;

/**
 * LinuxLptProvider
 *
 * @author  &lt;A HREF=&quot;mailto:lukas.zaruba@media-solutions.cz&quot;&gt;Lukas Zaruba&lt;/A&gt;, MEDIA SOLUTIONS CZECH REPUBLIC Ltd.
 * @version $Revision$ $Date$
 */
public class win32LptProvider implements ILptProvider {
	
	private static final String LIBRARY_NAME = "unianalyzerlpt";
	private static final Logger log = Logger.getLogger(win32LptProvider.class);
	
	private int address;
	private boolean opened;
	
	public void closeLptPort() throws UnianalyzerPortException {
		if (!opened) {
			log.info("Trying to close port \"" + address + "\" which is not opened...");
			return;
		}
		int i = closePort(address);
		if (i != 0) throw new UnianalyzerPortException("Error closing port \"" + address + "\" , error code " + i);
		opened = false;
		log.info("Port address \"" + address + "\" closed...");
	}

	public void openLptPort(int address) throws UnianalyzerPortException {
		this.address = address;
		if (opened) throw new UnianalyzerPortException("Port is already opened!!!");
		int i = win32LptProvider.openPort(address);
		if (i != 0) throw new UnianalyzerPortException("Error opening port \"" + address + "\" , error code " + i);
		opened = true;
		log.info("Port address \"" + address + "\" opened...");
	}

	public int readOneByte() throws UnianalyzerPortException {
		if (!opened) throw new UnianalyzerPortException("Port \"" + address + "\" is not opened!!!");
		int i = readByte(address);
		//Logger.log(IStatus.OK, "Retrieved byte \"" + i + "\" from port address \"" + address + "\"");
		return i;
	}
	
	public int[] readMoreBytes(int count, int delay) throws UnianalyzerPortException {
		if (!opened) throw new UnianalyzerPortException("Port \"" + address + "\" is not opened!!!");
		int[] result = readBytesWithDelay(address, count, delay);
		//Logger.log(IStatus.OK, "Retrieved bytes \"" + Arrays.toString(result) + "\" from port address \"" + address + "\"");
		return result;
	}
	
	public int[] readMoreBytesWithTrigger(int count, int delay, int triggerMask, int firstResult, int secondResult,
			long maxWaitingInterval) throws UnianalyzerPortException {
		if (!opened) throw new UnianalyzerPortException("Port \"" + address + "\" is not opened!!!");
		int[] result = readBytesWithDelayAndTrigger(address, count, delay, triggerMask, firstResult, secondResult, maxWaitingInterval);
		return result;
	}
	
	public static native int readByte(int address);
	
	public static native int[] readBytesWithDelay(int address, int count, int delay);
	
	public static native int[] readBytesWithDelayAndTrigger(int address, int count, int delay, int triggerMask, int firstResult, int secondResult, long maxWaitingInterval);
	   
	public static native int openPort(int address);
	
	public static native int closePort(int address);
	
    static {
    	log.info("Loading shared library \"" + LIBRARY_NAME + "\"");
    	try {
    		System.loadLibrary(LIBRARY_NAME);
    	} catch (UnsatisfiedLinkError e) {
    		log.error("Loading shared library \"" + LIBRARY_NAME + "\" failed.", e);
    	}
    }


}


/*
 * $Log$
 */